package com.framework.service.impl;

import com.alibaba.fastjson.JSON;
import com.framework.entity.flow.FlowTask;
import com.framework.service.AgentInfoService;
import com.framework.service.FlowTaskService;
import com.framework.service.PayCallbackService;
import com.framework.utils.pay.lianpay.base.PartnerConfig;
import com.framework.utils.pay.lianpay.util.YinTongUtil;
import com.framework.utils.pay.lianpay.vo.PayDataBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述: 连连支付回调
 *
 * @author ck
 * @date 2018/9/26 13:59
 */
@Service("payCallbackService")
public class PayCallbackServiceImpl  implements PayCallbackService {

	@Autowired
	private FlowTaskService flowTaskService;
	@Autowired
	private AgentInfoService agentInfoService;

	@Override
	public Map<String, Object> lianlianResultNotification(HttpServletRequest req) {
		System.out.println("进入连连付款异步通知数据接收处理");
		Map<String,Object> map = new HashMap<>();
		System.out.println("进入连连付款异步通知数据接收处理");
		String reqStr = YinTongUtil.readReqStr(req);
		if (YinTongUtil.isnull(reqStr)){
			//可抛异常，后续人工介入分析原因,这里的交易失败不是指订单付款失败，订单付款结果以订单付款状态字段result_pay为准
			map.put("ret_code", "9999");
			map.put("ret_msg", "交易失败");
			return map;
		}
		System.out.println("接收付款异步通知数据：【" + reqStr + "】");
		if (!YinTongUtil.checkSign(reqStr, PartnerConfig.YT_PUB_KEY,PartnerConfig.MD5_KEY)){
			//可抛异常，后续人工介入分析原因,这里的交易失败不是指订单付款失败，订单付款结果以订单付款状态字段result_pay为准
			map.put("ret_code", "9999");
			map.put("ret_msg", "交易失败");
			System.out.println("付款异步通知验签失败");
			return map;
		}
		map.put("ret_code", "0000");
		map.put("ret_msg", "交易成功");
		System.out.println("付款异步通知数据接收处理成功");
		//解析异步通知对象
		PayDataBean payDataBean = JSON.parseObject(reqStr, PayDataBean.class);
		String resultPay = payDataBean.getResult_pay();//订单状态
		String orderNo = payDataBean.getNo_order();//订单号
		String infoOrder = payDataBean.getInfo_order();//支付结果
		String moneyOrder = payDataBean.getMoney_order();//交易金额
		//查询订单信息
		FlowTask task = flowTaskService.queryFlowTaskByNo(orderNo);
		if(task != null){
			Long agentId = task.getAgentId(); //所属人
			BigDecimal amount = task.getTakeAmount();//提现金额
			if(StringUtils.equals("EXAMINE",task.getTaskState())) { //审核中的订单
				if("SUCCESS".equals(resultPay)){ //成功订单
					flowTaskService.updateStateByNo(orderNo,"SUCCESS","提现成功");
				}else{
					//FAILURE,CANCEL状态订单，统一作为失败订单处理
					flowTaskService.updateStateByNo(orderNo,resultPay,infoOrder);
					//账户余额回款
					agentInfoService.addBalance(agentId,amount);
				}
			}else{
				//审核中的订单，不用管
				map.put("ret_code", "9000");
				map.put("ret_msg", "交易审核");
			}
		}else{
			//没有订单，添加异常的订单信息
			FlowTask f = new FlowTask();
			f.setTakeNo(orderNo);
			f.setCreateTime(new Date());
			f.setExplains(infoOrder);
			f.setServiceAmount(new BigDecimal((moneyOrder)));
			f.setTaskState(resultPay);
			flowTaskService.insertFlowTask(f);
		}
		return map;
	}

}
